package sort;

/**
 * @author wcj
 * @Date 2021/7/7 1:33
 * @Version 1.0
 */
public class QuickSort {

    /**
     * 快排：每次选取一个基准值，比基准值小的放到一侧，比基准值大的放到另一侧
     *
     * @param arr
     * @param left
     * @param right
     */
    public static void quickSort(int[] arr, int left, int right) {
        if (left > right) {
            return;
        }
        int low = left;
        int high = right;
        int pivot = arr[left];
        while (low < high) {
            while (low < high && arr[high] >= pivot) {
                high--;
            }
            while (low < high && arr[low] <= pivot) {
                low++;
            }
            if (low < high) {
                int tmp = arr[low];
                arr[low] = arr[high];
                arr[high] = tmp;
            }
        }
        arr[left] = arr[low];
        arr[low] = pivot;
        quickSort(arr, left, low - 1);
        quickSort(arr, low + 1, right);
    }
}
